import pandas as pd


def merge_excel_columns(file1_path,  column_name, output_file=None):
    try:
        # 读取两个Excel文件
        df1 = pd.read_excel(file1_path)

        # 提取指定列
        if column_name not in df1.columns :
            raise ValueError(f"Column '{column_name}' not found in one or both Excel files")

        # 合并两列并去重
        unique_values = df1[column_name].drop_duplicates().dropna()

        print(len(unique_values))
        # 转换为逗号分隔的字符串
        result = ','.join(unique_values.astype(str))

        # 如果指定了输出文件，则保存结果
        if output_file:
            with open(output_file, 'w', encoding='utf-8') as f:
                f.write(result)

        return result

    except Exception as e:
        return f"Error: {str(e)}"


# 使用示例
file1_path = 'D:\\project_file\\20250918课程包标签处理\\复习课.xlsx'  # 第一个Excel文件路径
file2_path = 'D:\\project_file\\20250918课程包标签处理\\同步新授课.xlsx'  # 第二个Excel文件路径
column_name = 'resource_id'  # 要合并的列名
# output_file = 'output.txt'  # 可选：输出文件路径

result = merge_excel_columns(file2_path,  column_name)
print(result)